const svc = require('../util/service'),
  zoroDB = require('../util/zoroDB'),
  log = require('../util/log');

module.exports = (req, res) => {
  svc.handlePost(req, res, (reqObj, resolve) => {
    if (!reqObj.word) {
      resolve({
        result: -1,
        msg: '缺少word'
      })
      return
    }
    reqObj.word = reqObj.word.replace(
      /[^\u4e00-\u9fa5\w\d]/gi, '').toLowerCase();
    if (reqObj.word == '') {
      resolve({
        result: -1,
        msg: '缺少word'
      })
      return
    }
    zoroDB.query('select word, p from word where locate(?,word)>0 order by p desc', [reqObj.word], (err, rows, fields) => {
      if (err) {
        log(err);
        resolve({
          result: -1,
          rmsg: '系统错误'
        })
        return
      }
      resolve({
        result: 1,
        words: rows
      })
    })
  })
}
